<?php

class mcurl
{
    private static $curl=null;
    public $httpStatus;
    private $curlHandle;
    public $speed;
    public $timeOut = 60;

    public function __construct()
    {
        $this->curlHandle = curl_init();
        curl_setopt($this->curlHandle, CURLOPT_TIMEOUT, $this->timeOut);
    }

    public function setopt($key, $val)
    {
        curl_setopt($this->curlHandle, $key, $val);
    }

    public function get($url)
    {
        curl_setopt($this->curlHandle, CURLOPT_URL, $url);
        curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($this->curlHandle, CURLOPT_ENCODING, 'gzip,deflate');
        $result = curl_exec($this->curlHandle);
        $this->httpStatus = curl_getinfo($this->curlHandle);
        $this->speed = round($this->httpStatus['pretransfer_time'] * 1000, 2);
        return $result;
    }
    public static function new($keyv=null){
        if(self::$curl == null){self::$curl = new mcurl();}
        if(is_array($keyv)){
            foreach ($keyv as $k=>$v){
                self::setopt($k,$v);
            }
        }
        return self::$curl;
    }

    public function post($url, $data)
    {
        curl_setopt($this->curlHandle, CURLOPT_POST, 1);
        curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, $data);
        return $this->get($url);
    }
}